package com.gdqy.FCS.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;


import com.gdqy.FCS.common.api.FastResponse;
import com.gdqy.FCS.common.api.ListResponse;
import com.gdqy.FCS.common.mvc.Args;
import com.gdqy.FCS.common.api.Fileupload;
import com.gdqy.FCS.entity.Dictionary;
import com.gdqy.FCS.entity.Fabric;
import com.gdqy.FCS.entity.FabricComponent;
import com.gdqy.FCS.entity.Information;
import com.gdqy.FCS.entity.ProviderInfo;
import com.gdqy.FCS.entity.ProviderRange;
import com.gdqy.FCS.entity.User;
import com.gdqy.FCS.entity.Weave;
import com.gdqy.FCS.service.DictionaryService;
import com.gdqy.FCS.service.FabricComponentService;
import com.gdqy.FCS.service.FabricService;
import com.gdqy.FCS.service.InformationService;
import com.gdqy.FCS.service.ProviderInfoService;
import com.gdqy.FCS.service.ProviderRangeService;
import com.gdqy.FCS.service.UserService;
import com.gdqy.FCS.service.WeaveService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


@Controller
@RequestMapping("provider")
public class ProviderInfoController {
	@Autowired
	private ProviderInfoService providerInfoService;
	@Autowired
	private InformationService informationService;
	@Autowired
	private FabricComponentService fabricComponentService;
	@Autowired
	private DictionaryService dictionaryService;
	@Autowired
	private WeaveService weaveService;
	@Autowired
	private FabricService fabricService;
	@Autowired
	private ProviderRangeService providerRangeService;
	@Autowired
	private UserService userService;
	
	
	/******************************************************张伟键写的begin*******************************************************************/
	//跳转 到设计师个人中心
	@RequestMapping("provider")
	public ModelAndView provider(@RequestParam(value = "user_id", required = true) int user_id) {
		ModelAndView modelAndView = new ModelAndView();
		
		ProviderInfo providerInfo = providerInfoService.selectByUser_id(user_id);
		int provider_id = providerInfo.getId();
		//是否有该供应商
		if(providerInfo != null) {
			modelAndView.addObject("providerInfo", providerInfo);
			modelAndView.setViewName("provider/provider");
			//经营范围
		List<ProviderRange> providerRanges = providerRangeService.selectByProvider_id(provider_id);
		List<Dictionary> FA_list = new ArrayList<>();
		List<Dictionary> FP_list = new ArrayList<>();
		List<FabricComponent> FC_list = new ArrayList<>();
		List<Weave> weaves = new ArrayList<>();
		
		//遍历经营范围
			for(int temp = 0;temp<providerRanges.size();temp++) {
				int fabric_property = providerRanges.get(temp).getFabric_property();
				String code = providerRanges.get(temp).getProperty_code();
				if(fabric_property == 1) {
					//用途
					Dictionary fa = dictionaryService.selectByCode(code);
					FA_list.add(fa);
				}else if(fabric_property == 2) {
					//成分
					FabricComponent fabricComponent = fabricComponentService.selectByCode(code);
					FC_list.add(fabricComponent);
				}else if(fabric_property == 3) {
					//工艺
					Dictionary fp = dictionaryService.selectByCode(code);
					FP_list.add(fp);
				}else {
					//织法
					Weave weave = weaveService.selectByCode(code);
					weaves.add(weave);
				}
			}
			modelAndView.addObject("FA_list", FA_list);
			modelAndView.addObject("FC_list", FC_list);
			modelAndView.addObject("FP_list",FP_list );
			modelAndView.addObject("weaves",weaves);
			//资讯总数
			int information_count = informationService.selectCountByRelease_id(user_id);
			modelAndView.addObject("information_count", information_count);
			//面料总数
			int fabric_count = fabricService.selectCountByProvider_id(provider_id);
			modelAndView.addObject("fabric_count", fabric_count);
			modelAndView.setViewName("provider/provider_frist");
			return modelAndView;
		}
		
		modelAndView.addObject("msg", "不存在改供应商");
		modelAndView.setViewName("commonm/error");
		return modelAndView;
	}
	
	//处理修改信息的请求
	@RequestMapping("deal_changeInfo")
	public ModelAndView deal_changeInfo(int provider_id) {
		ModelAndView modelAndView = new ModelAndView();
		//获取供应商信息
		ProviderInfo providerInfo = providerInfoService.selectById(provider_id);
		modelAndView.addObject("providerInfo", providerInfo);
		
		//获取共供应范围
		List<ProviderRange> providerRanges = providerRangeService.selectByProvider_id(provider_id);
		List<Dictionary> FA_list = new ArrayList<>();
		List<Dictionary> FP_list = new ArrayList<>();
		List<FabricComponent> FC_list = new ArrayList<>();
		List<Weave> weaves = new ArrayList<>();
		
		//遍历经营范围
			for(int temp = 0;temp<providerRanges.size();temp++) {
				int fabric_property = providerRanges.get(temp).getFabric_property();
				String code = providerRanges.get(temp).getProperty_code();
				if(fabric_property == 1) {
					//用途
					Dictionary fa = dictionaryService.selectByCode(code);
					FA_list.add(fa);
				}else if(fabric_property == 2) {
					//成分
					FabricComponent fabricComponent = fabricComponentService.selectByCode(code);
					FC_list.add(fabricComponent);
				}else if(fabric_property == 3) {
					//工艺
					Dictionary fp = dictionaryService.selectByCode(code);
					FP_list.add(fp);
				}else {
					//织法
					Weave weave = weaveService.selectByCode(code);
					weaves.add(weave);
				}
			}
			modelAndView.addObject("FA_list", FA_list);
			modelAndView.addObject("FC_list", FC_list);
			modelAndView.addObject("FP_list",FP_list );
			modelAndView.addObject("",weaves);
			modelAndView.setViewName("path.....................................");
		return modelAndView;
	}
	//真正处理修改信息的请求

	@RequestMapping("change_info")
	public ModelAndView change_info(MultipartFile photo_pic,ProviderInfo providerInfo) throws IllegalStateException, IOException{
		ModelAndView modelAndView = new ModelAndView();
		User user = userService.selectById(providerInfo.getUser_id());
		providerInfo.setUpdate_time(new Date());
		String username= user.getUsername();
		String originalFilename =  photo_pic.getOriginalFilename();
		//头像处理
			//如果头像为空，不做处理,否则 修改
			if(photo_pic != null && originalFilename!=null && originalFilename!="" ){
				Fileupload fileupload = new Fileupload();
				String url=fileupload.upload(photo_pic, "provider", username, "provider-info");
				providerInfo.setPhoto_link(url);
				user.setPhoto_link(url);
				userService.update(user);
			}
			//个人资料处理
			providerInfoService.update(providerInfo);
			modelAndView.setViewName("provider/changeInfo");
		return modelAndView;
	}
	//浏览供应商
	@RequestMapping("scan_provider")
	public ModelAndView scan_provider() {
		ModelAndView modelAndView = new ModelAndView();
		
		//根据面料成分4种---找供应商
		//棉
		List<ProviderInfo> providerInfos_cotton = providerInfoService.selectByBig_code("COM001");
		modelAndView.addObject("providerInfos_cotton", providerInfos_cotton);
		//化纤
		List<ProviderInfo> providerInfos_chemicalFiber = providerInfoService.selectByBig_code("COM002");
		modelAndView.addObject("providerInfos_chemicalFiber", providerInfos_chemicalFiber);
		//皮毛
		List<ProviderInfo> providerInfos_fur = providerInfoService.selectByBig_code("COM003");
		modelAndView.addObject("providerInfos_fur", providerInfos_fur);
		//混纺
		List<ProviderInfo> providerInfos_blending = providerInfoService.selectByBig_code("COM001");
		modelAndView.addObject("providerInfos_blending", providerInfos_blending);
		
		modelAndView.setViewName("provider/provider_allList");
		return modelAndView;
	}

	/******************************************************张伟键写的end*******************************************************************/
	/*
	 * 
	 * 返回供应商浏览列表视图
	 * 
	 **/ 
	@RequestMapping("providerInfo")
	public ModelAndView getProviderInfoList(Args args,int pageindex, int pagesize) {
		ModelAndView modelAndView = new ModelAndView();
		//供应商信息
		PageHelper.startPage(pageindex, pagesize);
		List<ProviderInfo> providerInfoList=providerInfoService.selectAll();
		PageInfo<ProviderInfo> page = new PageInfo<ProviderInfo>(providerInfoList);
		 ListResponse<ProviderInfo> json= FastResponse._().errCode(0).subCode(0).errMsg("分页成功")
				.args(args.getReqeustargs()).pageIndex(page.getPageNum())
				.pageSize(page.getSize()).total((int)page.getTotal()).pageCount(page.getPages())
				.hasNext(page.isHasNextPage()).list(providerInfoList);
		 modelAndView.addObject("json", json);
		//人气资讯
		List<Information> infoBytimeList=informationService.selectByCount();
		//人气供应商
		ProviderInfo providerInfo=new ProviderInfo();
		List<ProviderInfo> providerInfoBytimeList=providerInfoService.selectByTime(providerInfo);
		int providerCount=providerInfoService.selectAllCount();
		modelAndView.addObject("providerInfoList",providerInfoList);
		modelAndView.addObject("infoBytimeList",infoBytimeList);
		modelAndView.addObject("providerInfoBytimeList",providerInfoBytimeList);
		modelAndView.addObject("providerCount",providerCount);
		modelAndView.setViewName("provider/provider");
		return modelAndView;

	}
	
	
	/* 
	 * 返回供应商个人中心视图
	 * 
	 * 
	@RequestMapping("getProviderCenter")
	public ModelAndView getProviderCenter() {
		ModelAndView modelAndView = new ModelAndView();		
		//供应商信息
		
		modelAndView.setViewName("provider/providerindividual");
		return modelAndView;

	}
	
	@RequestMapping("addFabric")
	public ModelAndView addFabric(@RequestParam(value = "provider_id",required = true) int provider_id){
		ModelAndView modelAndView = new ModelAndView();
		//面料成分
		List<FabricComponent> componentList=fabricComponentService.selectAll();
		//面料用途
		List<Dictionary> applicationList=dictionaryService.selectByType_code("ZD007");
		//面料工艺
		List<Dictionary> productionList=dictionaryService.selectByType_code("ZD008");
		//面料织法
		List<Weave> weaveList=weaveService.selectAll();
		
		modelAndView.addObject("componentList", componentList);
		modelAndView.addObject("applicationList", applicationList);
		modelAndView.addObject("productionList", productionList);
		modelAndView.addObject("weaveList", weaveList);
		modelAndView.addObject("provider_id", provider_id);
		modelAndView.setViewName("provider/provider_addFabric");
		return modelAndView;	
	}
	
	@RequestMapping("fabricComponentChild")
	public List<FabricComponent> fabricComponentChild(@RequestParam("code") String code) {
		List<FabricComponent> componentList=fabricComponentService.selectClass(code);
		for(FabricComponent fabricComponent:componentList){
			fabricComponent.setCode(code);
		}
		return componentList;
	}*/
	
	
	 /* 
	 * 返回供应商详细页面视图
	 * 
	 * */
	@RequestMapping("providerDetail")
	public ModelAndView getProviderDetail(@RequestParam(value = "id",required = true) int id) {
		ModelAndView modelAndView = new ModelAndView();
		//人气面料 
		List<Fabric> collectedList=fabricService.selectFabircByCollected(id);
		//最新面料
		List<Fabric> newList=fabricService.selectFabircByReleasetime(id);
		
		ProviderInfo provider=providerInfoService.selectById(id);
		modelAndView.addObject("provider", provider);
		modelAndView.addObject("collectedList", collectedList);
		modelAndView.addObject("newList", newList);
		modelAndView.setViewName("provider/providerDetail");
		return modelAndView;
	}
}
